package com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.dao;

import com.skytech.tax.customsdatawarehouse.singlewindow.taxhandle.domain.entity.TaxBill;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Lianhong
 * @since 2022/4/15
 */
@Repository
public class TaxBillDao {

    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private String insertTaxBillSql = "INSERT INTO sw_tax_bill (swTaxId, taxHeadSeqNo, entryId, taxvouNo, taxBill, socialCreditCode, storageTime, payType, genDateStr)" +
            " VALUES (:swTaxId, :taxHeadSeqNo, :entryId, :taxvouNo, :taxBill, :socialCreditCode, :storageTime, :payType, :genDateStr)";

    public void save(List<TaxBill> taxBillList) {
        SqlParameterSource[] batch = SqlParameterSourceUtils.createBatch(taxBillList);
        namedParameterJdbcTemplate.batchUpdate(insertTaxBillSql, batch);
    }

    private String deleteTaxBillSql = "DELETE FROM sw_tax_bill WHERE taxHeadSeqNo IN (:taxHeadSeqNos)";

    public void deleteBy(List<String> taxHeadSeqNos) {
        Map<String, Object> params = new HashMap<>();
        params.put("taxHeadSeqNos", taxHeadSeqNos);
        namedParameterJdbcTemplate.update(this.deleteTaxBillSql, params);
    }
}
